<?php

namespace App\Admin\Controllers;

use App\Models\Messages;
use Carbon\Carbon;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class MessagesController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '咨询列表';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Messages());
        $grid->model()->orderBy('id','desc');
        $grid->column('id', '编号')->sortable()->width(50);
        $grid->column('name', '名称')->width(80);
        $grid->column('mobile', '手机号')->width(120);
        $grid->column('target', '咨询方向')->width(200);
        $grid->column('content', '咨询内容');
        $grid->column('created_at', '咨询时间')->width(200)->display(function ($time) {
            return Carbon::parse($time)->setTimezone('Asia/Shanghai')->toDateTimeString();
        });
        $grid->disableActions();
        $grid->disableCreateButton();
        $grid->disableRowSelector();


        $grid->filter(function ($filter) {
            $filter->disableIdFilter();
            $filter->between('created_at', '咨询时间')->datetime();
            $filter->like('mobile','手机号');
            $filter->like('name','名称');
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed   $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Messages::findOrFail($id));

        $show->field('id', __('ID'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Messages);

        $form->display('id', __('ID'));
        $form->display('created_at', __('Created At'));
        $form->display('updated_at', __('Updated At'));

        return $form;
    }
}
